package com.sptpc.getactivityresult;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button)findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              Intent intent = new Intent(MainActivity.this,TargetActivitiy.class);
              startActivityForResult(intent,1);
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Toast.makeText(this, "回调成功", Toast.LENGTH_SHORT).show();
        if (requestCode == 1) {
            switch (resultCode) {
                case 2:
                    Toast.makeText(this, "你选择了保存", Toast.LENGTH_SHORT).show();
                    break;
                case 3:
                    Toast.makeText(this, "你选择了不保存", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    }
}
